/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.basic.services;

import javax.annotation.Resource;

import junit.framework.Assert;
import com.eelpo.cmmp.basic.dao.vo.SystemConfig;
import com.eelpo.cmmp.core.testcase.BasicCommonServiceTestCase;

import org.junit.Test;

/**
 * <pre>
 * 
 * 系统配置测试用例
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class SystemConfigManageServiceTestCase extends BasicCommonServiceTestCase {

	@Resource
	private ISystemConfigManageService systemConfigManageService;

	// 键值都为空新增测试
	@Test
	public void addSystemconfigByNull() {
		try {
			systemConfigManageService.addSystemConfig(new SystemConfig());
			Assert.fail("系统配置键值不能为空");
		} catch (Exception e) {
			Assert.assertTrue(true);
		}
	}

	// 值为空新增测试
	@Test
	public void addSystemConfigByNullKey() {
		SystemConfig config = new SystemConfig();
		try {
			config.setConfigKey("config_key");
			systemConfigManageService.addSystemConfig(config);
			Assert.fail("系统配置值不能为空");
		} catch (Exception e) {
			Assert.assertTrue(true);
		}
	}

	// 正确的新增测试
	@Test
	public void addSystemConfig() {
		SystemConfig config = new SystemConfig();
		try {
			config.setConfigKey("config_key");
			config.setConfigValue("config_value");
			systemConfigManageService.addSystemConfig(config);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}

	}

	// 重复新增测试
	@Test
	public void addSystemConfigByRepeat() {
		SystemConfig config = new SystemConfig();
		try {
			config.setConfigKey("key");
			config.setConfigValue("value");
			systemConfigManageService.addSystemConfig(config);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}

		try {
			config.setId(null);
			systemConfigManageService.addSystemConfig(config);
			Assert.fail("系统配置已经存在");
		} catch (Exception e) {
			Assert.assertTrue(true);
		}
	}

	// 修改值测试
	@Test
	public void updateSystemConfigValue() {
		SystemConfig config = new SystemConfig();
		try {
			config.setConfigKey("key1");
			config.setConfigValue("value1");
			systemConfigManageService.addSystemConfig(config);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}

		try {
			config.setConfigValue("config_new_value");
			systemConfigManageService.updateSystemConfig(config);
			Assert.assertEquals("config_new_value", config.getConfigValue());
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}
	}

	// 修改重复键测试
	@Test
	public void addSystemConfigRepeatKey() {

		SystemConfig config = new SystemConfig();
		try {
			config.setConfigKey("key2");
			config.setConfigValue("value1");
			systemConfigManageService.addSystemConfig(config);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}

		SystemConfig config1 = new SystemConfig();
		try {
			config1.setConfigKey("key3");
			config1.setConfigValue("value1");
			systemConfigManageService.addSystemConfig(config1);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}

		try {
			config1.setConfigKey("key2");
			systemConfigManageService.updateSystemConfig(config1);
			Assert.fail("系统配置键已经存在");
		} catch (Exception e) {
			Assert.assertTrue(true);
		}

	}

	// 删除测试
	@Test
	public void deleteSystemConfig() {

		SystemConfig config = new SystemConfig();
		try {
			config.setConfigKey("key4");
			config.setConfigValue("value1");
			systemConfigManageService.addSystemConfig(config);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}

		try {
			systemConfigManageService.deleteSystemConfig(config);
		} catch (Exception e) {
			e.printStackTrace();
			Assert.assertTrue(false);
		}
	}
}
